﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using DAL = AccessDAL;
using CommonFunc;
using DBUtility;

namespace BLL
{
    public class INews
    {
        DAL.INewsTableAdapters.INewsTableAdapter DIN =
            new DAL.INewsTableAdapters.INewsTableAdapter();

        MODEL.INews MIN = new MODEL.INews();
        MODEL.INews _MIN = new MODEL.INews();
        MODEL.INewsClass MINC = new MODEL.INewsClass();
        MODEL.INewsClass _MINC = new MODEL.INewsClass();

        BLL.INewsClass BINC = new INewsClass();


        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="MIN"></param>
        /// <returns></returns>
        public MODEL.INews GetDataByID(MODEL.INews MIN)
        {
            try
            {
                DataTable DT = DIN.GetDataByID(MIN.IN_ID);

                _MIN.IN_ClassCode = DT.Rows[0]["IN_ClassCode"].ToString();
                _MIN.IN_ClassID = Convert.ToInt32(DT.Rows[0]["IN_ClassID"]);
                _MIN.IN_Content = DT.Rows[0]["IN_Content"].ToString() ;
                _MIN.IN_DateTime = Convert.ToDateTime(DT.Rows[0]["IN_DateTime"]);
                _MIN.IN_Hits = Convert.ToInt32(DT.Rows[0]["IN_Hits"]);
                _MIN.IN_ID = Convert.ToInt32(DT.Rows[0]["IN_ID"]);
                _MIN.IN_Index = Convert.ToBoolean(DT.Rows[0]["IN_Index"]);
                _MIN.IN_IsDel = Convert.ToBoolean(DT.Rows[0]["IN_IsDel"]);
                _MIN.IN_IsLock = Convert.ToBoolean(DT.Rows[0]["IN_IsLock"]);
                _MIN.IN_MasterID = Convert.ToInt32(DT.Rows[0]["IN_MasterID"]);
                _MIN.IN_Order = Convert.ToInt32(DT.Rows[0]["IN_Order"]);
                _MIN.IN_SmallThumb = DT.Rows[0]["IN_SmallThumb"].ToString();
                _MIN.IN_Thumbnail = DT.Rows[0]["IN_Thumbnail"].ToString();
                _MIN.IN_Title = DT.Rows[0]["IN_Title"].ToString();

                return _MIN;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加图文
        /// <summary>
        /// 添加图文
        /// </summary>
        /// <param name="MIN"></param>
        public void Add(MODEL.INews MIN)
        {
            try
            {
                DataTable DT_GetDataByClassID_Title = DIN.GetDataByClassID_Title(MIN.IN_Title, MIN.IN_ClassID);
                if (DT_GetDataByClassID_Title.Rows.Count != 0)
                {
                    throw new Exception("同一类别下，不能有重复标题");
                }

                MINC.INC_ID = MIN.IN_ClassID;
                _MINC = BINC.GetDataByID(MINC);
                string ClassCode = _MINC.INC_ClassCode;

                int OrderNum;
                object TempOrderNum = DIN.GetMaxOrder();
                if (TempOrderNum == null)
                {
                    OrderNum = 1;
                }
                else
                {
                    OrderNum = Convert.ToInt32(TempOrderNum) + 1;
                }
                

                DIN.Add(MIN.IN_Title, MIN.IN_ClassID, ClassCode, MIN.IN_Thumbnail, MIN.IN_SmallThumb,
                    MIN.IN_Content, MIN.IN_Hits, MIN.IN_MasterID, MIN.IN_DateTime, MIN.IN_IsLock, MIN.IN_IsDel,
                    OrderNum, MIN.IN_Index);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改图文
        /// <summary>
        /// 修改图文
        /// </summary>
        /// <param name="MIN"></param>
        public void Mod(MODEL.INews MIN)
        {
            try
            {
                MINC.INC_ID = MIN.IN_ClassID;
                _MINC = BINC.GetDataByID(MINC);

                string ClassCode = _MINC.INC_ClassCode;

                DIN.Mod(MIN.IN_Title, MIN.IN_ClassID, ClassCode, MIN.IN_Content,
                    MIN.IN_MasterID, MIN.IN_IsLock, MIN.IN_Index, MIN.IN_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除图文
        /// <summary>
        /// 删除图文
        /// </summary>
        /// <param name="MIN"></param>
        public void Del(MODEL.INews MIN)
        {
            try
            {
                _MIN = GetDataByID(MIN);
                string Thumb = HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + _MIN.IN_Thumbnail;
                string SmallThumb = HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + _MIN.IN_SmallThumb;

                DIN.Del(MIN.IN_ID);

                CommonFunction.DelFile(Thumb);
                CommonFunction.DelFile(SmallThumb);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改图片
        /// <summary>
        /// 修改图片
        /// </summary>
        /// <param name="MIN"></param>
        public void ModImage(MODEL.INews MIN)
        {
            try
            {
                _MIN = GetDataByID(MIN);
                string Thumb = HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + _MIN.IN_Thumbnail;
                string SmallThumb = HttpContext.Current.Server.MapPath("~/") + "UploadImage/" + _MIN.IN_SmallThumb;

                DIN.ModImage(MIN.IN_Thumbnail, MIN.IN_SmallThumb, MIN.IN_ID);

                CommonFunction.DelFile(Thumb);
                CommonFunction.DelFile(SmallThumb);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 标记已删除
        /// <summary>
        /// 标记已删除
        /// </summary>
        /// <param name="MIN"></param>
        public void MarkIsDel(MODEL.INews MIN)
        {
            try
            {
                string SQLstr_IsDel = "UPDATE [INews] SET [IN_IsDel]=NOT [IN_IsDel] WHERE [IN_ID]=" + MIN.IN_ID;

                AccHelper.ExecuteSQL(SQLstr_IsDel);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
